package assg.funnycells.util;

import java.util.Hashtable;
import java.io.*;

public class Configuration {
    Hashtable<String, String> config;
    Logger logger;
    
    public Configuration(String configPath) {
	config = new Hashtable<String, String>();
	logger = Logger.getDefaultLogger();
	
	read(configPath);
    }

    public void read(String configPath) {
	try {	    
	    BufferedReader in = new BufferedReader(new FileReader(configPath));
	    String readline;
	    String context = "", key = "", val = "";
	    
	    while((readline = in.readLine()) != null) {
		readline.trim();
		if(readline.startsWith("#")) {
		    continue;
		} else if(readline.startsWith("[")) {
		    context = StringOperations.parseContext(readline);		
		} else if(!readline.equals("")) {
		    key = StringOperations.parseKey(readline);
		    val = StringOperations.parseVal(readline);
		    if(!context.equals("") && !key.equals("")) {
			config.put(key = uniqueKey(context, key), val);
			logger.info("Config: " + key + " = " + val);
		    }
		}
	    }
	    
	    in.close();
	} catch(Exception e) {
	    logger.error("Unable to read config file", e);
	}
    }

    public String getVal(String context, String key) {
	return config.get(uniqueKey(context, key));
    }

    public int getIntVal(String context, String key) {
	try {
	    return Integer.parseInt(getVal(context, key));
	} catch(Exception e) {
	    return -1;
	}
    }

    public double getDoubleVal(String context, String key) {
	try {
	    return Double.parseDouble(getVal(context, key));
	} catch(Exception e) {
	    return -1;
	}
    }

    private String uniqueKey(String context, String key) {
	return (context + "." + key).toLowerCase();
    }

    public final static String SERVER = "server";
    public final static String IP = "ip";
    public final static String PORT = "port";

    public final static String MAP = "map";
    public final static String HEIGHT = "height";
    public final static String WIDTH = "width";
    public final static String NUMENERGY = "numenergy";

    public final static String CLASSES = "classes";
    public final static String SUSCEPTIBLECELL = "susceptiblecell";
    public final static String INFECTEDCELL = "infectedcell";
    public final static String ANTIBODYCELL = "antibodycell";
    public final static String NEWINFECTEDCELL = "newinfectedcell";
    public final static String NEWANTIBODYCELL = "newantibodycell";    
    public final static String ENERGYCELL = "energycell";

    public final static String PARAMS = "params";
    public final static String SENSERADIUS = "senseradius";
    public final static String MOLTIMEOUT = "moltimeout";
    public final static String RATETIMEOUT = "ratetimeout";
    public final static String DROPTIMEOUT = "droptimeout";
    public final static String PHEROMONETIMEOUT = "pheromonetimeout";
    public final static String JIGGLE = "jiggle";
    public final static String MOLENERGY = "molenergy";
    public final static String MOVENERGY = "movenergy";
    public final static String INFECTIONENERGY = "infectionenergy";
    public final static String IMMUNISATIONENERGY = "immunisationenergy";
    public final static String TRANSFERENERGY = "transferenergy";
    public final static String MAXENERGY = "maxenergy";
    public final static String SENSEENERGY = "senseenergy";
    public final static String QUORUMSIZE = "quorumsize";
    public final static String UPDATEWINDOW = "updatewindow";
    public final static String ENERGYALPHA = "energyalpha";

    public final static String RECEPTORS = "receptors";
    public final static String START = "start";
    public final static String STOP = "stop";
    public final static String ANY = "any";
    public final static String INFECTED = "infected";
    public final static String ANTIBODY = "antibody";

    public final static String AFFECTORS = "affectors";
    public final static String INFECTION = "infection";
    public final static String IMMUNISATION = "immunisation";
    public final static String PRESENCE = "presence";
    public final static String PHEROMONE = "pheromone";
    public final static String ENERGY = "energy";

    public final static String COLORS = "colors";

    public final static String DISPLAY = "display";
    public final static String DISPHEIGHT = "dispheight";
    public final static String DISPWIDTH = "dispwidth";
}